package com.itheima.mm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mm.constant.MessageConstant;
import com.itheima.mm.context.BaseContext;
import com.itheima.mm.dto.CatalogDTO;
import com.itheima.mm.entity.CatalogPageQueryParams;
import com.itheima.mm.entity.PageResult;
import com.itheima.mm.entity.QueryPageBean;
import com.itheima.mm.entity.Result;
import com.itheima.mm.exception.BaseException;
import com.itheima.mm.mapper.CatalogMapper;
import com.itheima.mm.mapper.QuestionMapper;
import com.itheima.mm.mapper.UserMapper;
import com.itheima.mm.pojo.Catalog;
import com.itheima.mm.pojo.User;
import com.itheima.mm.service.CatalogService;
import com.itheima.mm.utils.DateUtils;
import com.itheima.mm.vo.CatalogVO;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description CatalogServiceImpl
 * @Author mylee
 * @Date 2023-09-27
 */
@Service
public class CatalogServiceImpl implements CatalogService {
    @Resource
    private CatalogMapper catalogMapper;
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private UserMapper userMapper;

    /**
     * 学科目录新增
     *
     * @return
     * @param: catalogDTO
     **/
    @Override
    public void add(CatalogDTO catalogDTO) {
        Catalog catalog = new Catalog();
        BeanUtils.copyProperties(catalogDTO, catalog);

        catalog.setCreateDate(DateUtils.parseDate2String(new Date()));
        catalog.setStatus(0);
        catalog.setCourseId(BaseContext.getCurrentId());
        catalogMapper.insert(catalog);
    }

    /**
     * 学科目录分页列表
     *
     * @return
     * @param: queryPageBean
     **/
    @Override
    public PageResult<CatalogVO> findListByPage(QueryPageBean<CatalogPageQueryParams> queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<CatalogVO> list = catalogMapper.findListByPage(queryPageBean.getQueryParams());
        Page<CatalogVO> page = (Page<CatalogVO>) list;

        List<Catalog> rows = list.stream().map(catalogVO -> {
            Catalog catalog = new Catalog();
            BeanUtils.copyProperties(catalogVO, catalog);
            Integer courseId = catalog.getCourseId();
            Integer userId = catalog.getUserId();
            String Creator = userMapper.findByUserId(userId);
            Integer questionQty = questionMapper.countByQuestion(courseId);

            catalog.setCreator(Creator);
            catalog.setCreateDate(DateUtils.parseDate2String(new Date()));
            catalog.setQuestionQty(questionQty);

            return catalog;
        }).collect(Collectors.toList());
        return new PageResult(page.getTotal(), rows);
    }

    /**
     * 学科目录删除
     *
     * @return
     * @param: catalogId
     **/
    @Override
    public void delete(Integer catalogId) {
        Integer count = questionMapper.countByQuestion(catalogId);
        if (count > 0) {
            throw new BaseException(MessageConstant.QUESTION_BY_SALE);
        }
        catalogMapper.delete(catalogId);
    }

    /**
     * 学科目录更新
     *
     * @return
     * @param: catalogDTO
     **/
    @Override
    public void update(CatalogDTO catalogDTO) {
        Catalog catalog = new Catalog();
        BeanUtils.copyProperties(catalogDTO,catalog);
        Integer currentId = BaseContext.getCurrentId();
        catalog.setUserId(currentId);
        catalogMapper.update(catalog);
    }
}
